package com.wang.ddp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.wang.ddp.dto.Result;
import com.wang.ddp.entity.Shop;
import com.wang.ddp.entity.ShopType;
import com.wang.ddp.mapper.ShopTypeMapper;
import com.wang.ddp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Autowired
    StringRedisTemplate redisTemplate;
    @Override
    public List<ShopType> getTypeList() {
        //先从redis中查询数据
        String typeListJson = redisTemplate.opsForValue().get("cache:type:list");
        //如果redis中有数据就直接返回数据
        if (StrUtil.isNotBlank(typeListJson)){
            List<ShopType> list = JSONUtil.toList(typeListJson, ShopType.class);
            return list;
        }
        //如果redis中没有就从数据库中查询，判空并将查询的所有数据存入到redis中
        List<ShopType> list = this.list();
        if (list!=null){
            redisTemplate.opsForValue().set("cache:type:list",JSONUtil.toJsonStr(list),30L, TimeUnit.MINUTES);
        }
        return list;
    }
}
